package top.gongtao.tencent.cos.tencentcosweb.qcloudclient;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.request.UploadSliceFileRequest;
import com.qcloud.cos.sign.Credentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * Created by gongtao on 2017/11/18.
 */

@Component
public class QcloudClient {

    private static Logger log = LoggerFactory.getLogger(QcloudClient.class);

    static int APP_ID = 1253652570;
    static String SECRET_ID = "AKIDQg7lR27hleLqNmH7Wip04xoUyGMIqawu";
    static String SECRET_KEY = "63qRr57yeSEpHXfeQUPOHz8EFuVSKE3h";
    static String bucketName = "myfirstbucket";

    // 初始化秘钥信息
    static Credentials cred = new Credentials(APP_ID, SECRET_ID, SECRET_KEY);

    // 初始化客户端配置
    static ClientConfig clientConfig = new ClientConfig();

    // 初始化cosClient
    static  COSClient cosClient = new COSClient(clientConfig, cred);


    /**
     * 自动判断文件的大小,已使用是否是分片的方式上传文件
     * @param remoteFolderPath 远程文件夹的名称
     * @param loaclPath        文件的本地路径
     * @return
     */
    public static String uploadFile(String remoteFolderPath, String loaclPath) {
        //获取文件输入流
        File f = new File(loaclPath);
        String result = null;
        if (f.exists() && f.isFile()) {
            long size = f.length();
            if (size <= 8 * 1024 * 1024) { //小于等于8M
                result = cosUploadFile(remoteFolderPath + getFileName(loaclPath), loaclPath);
            } else {
                result = sliceUploadFile(remoteFolderPath + getFileName(loaclPath), loaclPath);
            }
        } else {
            System.out.println("file doesn't exist or is not a file");  // 文件不存在或不是一个文件
        }
        return result;
    }

    /**
     * 上传一个长度为10个字节的文件
     * @param remotePath
     * @param localPath
     * @return
     */
    public static String cosUploadFile(String remotePath, String localPath) {
        // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
        clientConfig.setRegion("sh");
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, remotePath, localPath);
        String result = null;
        try {
            result = cosClient.uploadFile(uploadFileRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("uploadFile result:" + result);
        return result;
    }

    /**
     * 串行分片上传文件, 使用默认分片大小
     * @param remotePath
     * @param localPath
     * @return
     */
    public static String sliceUploadFile(String remotePath, String localPath) {
        // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
        clientConfig.setRegion("sh");
        UploadSliceFileRequest uploadFileRequest = new UploadSliceFileRequest(bucketName, remotePath, localPath,1048576);
        String result = null;
        try {
            result = cosClient.uploadSliceFile(uploadFileRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("sliceUploadFile result: " + result);
        return result;
    }


    /**
     * 根据本地文件的路径获取文件的名字
     * @param localPath 本地文件路径
     * @return 文件的名称
     */
    public static String getFileName(String localPath) {
        String[] result = localPath.split("\\/");
        return result[result.length - 1];
    }
}